package com.wtkl.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class baseServlet extends HttpServlet{


    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

//        获取传来的方法名字，要知晓需要使用哪个方法
        String methodName = req.getParameter("method");


        Method method = null;
        try {

            Class<? extends baseServlet> aClass = this.getClass();
            method = aClass.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);

            /*
             * this：当前类，使用service方法的类，这里是继承BaseServlet的类
             * method.invoke在this这个类中调用method方法
             */

            method.invoke(this, req, resp);

        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }


    }
}
